package com.acoffee.config;

import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * @author acoffee
 * @create 2021-08-05 11:09
 */
public class MyLocalResolver implements LocaleResolver {

    //解析请求
    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
        //获取请求中的语言参数：请求里面带了l的都会走这个请求
        String language = httpServletRequest.getParameter("l");//解析参数这个l来自th:href="@{/index.html(l='zh_CN')}"从而实现解析
        Locale locale = Locale.getDefault();//如果没有就使用默认的，即login.properties文件中的

        //如果请求的连接携带了国际化的参数
        if (!StringUtils.isEmpty(language)) {
            //zh_CN
            String[] split = language.split("_");//通过字符串分割从而去匹配国家与地区
            //国家地区
            locale = new Locale(split[0], split[1]);

        }

        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }
}
